const fs = require('fs');
const path = require('path');

// 指定要读取和重命名的目录路径
const directoryPath = 'C:/Users/Administrator/Desktop/图标'; // 替换为你的目录路径

// 定义name到value的映射
const items = [
    { name: "中间件", value: "widgetMiddleware" },
    { name: "光纤交换机", value: "widgetOptical_Fiber_Switch" },
    { name: "动环设备", value: "widgetEnvironmental_Equipment" },
    { name: "存储", value: "widgetStorage" },
    { name: "操作系统", value: "widgetOperating_System" },
    { name: "数据库", value: "widgetDatabase" },
    { name: "无线", value: "widgetWireless" },
    { name: "无线_AC", value: "widgetWireless_AC" },
    { name: "无线_ap", value: "widgetWireless_AP" },
    { name: "标准应用", value: "widgetStandard_Application" },
    { name: "硬件服务器", value: "widgetHardware_Server" },
    { name: "示意节点", value: "widgetDemonstration_Node" },
    { name: "线路", value: "widgetLine" },
    { name: "网络设备", value: "widgetNetwork_Equipment" },
    { name: "虚拟化", value: "widgetVirtualization" },
    { name: "虚拟化_交换机", value: "widgetVirtualization_Switch" },
    { name: "虚拟化_存储", value: "widgetVirtualization_Storage" },
    { name: "虚拟化_宿主机", value: "widgetVirtualization_Host" },
    { name: "虚拟化_虚拟机", value: "widgetVirtualization_VM" },
    { name: "虚拟化_集群", value: "widgetVirtualization_Cluster" }
];

// 获取目录中的文件列表
fs.readdir(directoryPath, (err, files) => {
    if (err) {
        console.log('读取目录失败:', err);
        return;
    }

    // 遍历目录中的文件
    files.forEach(file => {
        // 获取文件名（不含扩展名）
        const fileName = path.parse(file).name;

        // 查找与文件名匹配的项
        const match = items.find(item => fileName.includes(item.name));

        if (match) {
            // 拼接新的文件名（包括 .svg 后缀）
            const newFileName = `${match.value}.svg`;

            // 拼接文件的完整路径
            const oldFilePath = path.join(directoryPath, file);
            const newFilePath = path.join(directoryPath, newFileName);

            // 重命名文件
            fs.rename(oldFilePath, newFilePath, (err) => {
                if (err) {
                    console.log(`重命名失败: ${file} -> ${newFileName}`, err);
                } else {
                    console.log(`文件重命名成功: ${file} -> ${newFileName}`);
                }
            });
        }
    });
});
